package org.deng.vertx.springboot.config;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.auth.KeyStoreOptions;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.auth.jwt.JWTAuthOptions;
import io.vertx.ext.web.handler.JWTAuthHandler;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * @author 邓力宾
 * @date 2022/12/28 16:23
 * @desc
 */
@SpringBootConfiguration
public class VertxConfig {

    @Bean
    public Vertx vertx(){
        VertxOptions options = new VertxOptions();
        //最大工作线程(运行阻塞代码)数 默认20
        options.setWorkerPoolSize(10);
        //事件循环线程数（默认2*cpu核心数）
        //options.setEventLoopPoolSize(4);
        return Vertx.vertx(options);
    }

    /**
     * jwt
     * @param vertx
     * @return
     */
    @Bean
    public JWTAuth jwtAuth(Vertx vertx){
        JWTAuthOptions authConfig = new JWTAuthOptions()
                .setKeyStore(new KeyStoreOptions()
                        .setPassword("si99S0%4P09JS=)2"));

        return JWTAuth.create(vertx, authConfig);
    }

    /**
     * 登录验证  请求头 authorization = Bearer [token]
     * @param jwtAuth
     * @return
     */
    @Bean
    public JWTAuthHandler jwtAuthHandler(JWTAuth jwtAuth){
        return JWTAuthHandler.create(jwtAuth);
    }
}
